layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/baseJob/jsonList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'jobName', title: '任务名',align:'center'},
            {field: 'jobGroup', title: '任务组',align:'center'},
            {field: 'cronExpression', title: '表达式',align:'center'},
            {field: 'status', title: '状态', align:'center', minWidth:110, templet:function(d){
                if(d.status=='0'){
                    return '正常';
                }else {
                    return '暂停';
                }
            }},
            {title: '操作',width:220,align:"center", templet:function (d) {
                var jobBtn = '';
                if (d.status == 1) {
                    jobBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="resume">启用</a>';
                } else {
                    jobBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="pause">暂停</a>';
                }
                var runBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="run">执行</a>';
                var editBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                return jobBtn + runBtn + editBtn + delBtn;
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                username: $(".searchVal").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        if(params==null||params==""){
            title="添加"
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            area: ['430px', '520px'], //宽高
            content : "/baseJob/selectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                setTimeout(function(){
                    layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })

    }
    $(".add_btn").click(function(){
        addEntity('');
    })

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addEntity("?id="+data.id);
        }else if(layEvent === 'resume'){ //启用
            $.get("/baseJob/resumeJob",{
                "id":data.id
            },function(result){
                if(1==result.returnInfo.state){
                    tableIns.reload();
                    layer.close(index);
                }else{
                    top.layer.msg(result.returnInfo.errorMsg);
                }
            })
        }else if(layEvent === 'pause'){ //暂停
            $.get("/baseJob/pauseJob",{
                "id":data.id
            },function(result){
                if(1==result.returnInfo.state){
                    tableIns.reload();
                    layer.close(index);
                }else{
                    top.layer.msg(result.returnInfo.errorMsg);
                }
            })
        }else if(layEvent === 'run'){
            $.get("/baseJob/run",{
                "id":data.id
            },function(result){
                if(1==result.returnInfo.state){
                    tableIns.reload();
                    layer.close(index);
                }else{
                    top.layer.msg(result.returnInfo.errorMsg);
                }
            })
        } else if(layEvent === 'del'){ //删除
            layer.confirm('是否删除该定时任务？',{icon:3, title:'提示信息'},function(index){
                 $.get("/baseJob/DeleteEntity",{
                     "id":data.id,
                     "_method":"DELETE"
                 },function(result){
                     if(1==result.returnInfo.state){
                         tableIns.reload();
                         layer.close(index);
                     }else{
                         top.layer.msg(result.returnInfo.errorMsg);
                     }
                 })
            });
        }
    });

})